package com.example.demo.ObjectClone;

import java.io.*;

public class MyUtil {
    private MyUtil(){
        throw new AssertionError();
    }
    public static <T extends Serializable> T clone(T obj) throws Exception{
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bout);
        oos.writeObject(obj);

        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
        ObjectInputStream oin = new ObjectInputStream(bin);
        return (T)oin.readObject();
    }
}
